package com.ebupt.migu.music.bannertwo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebupt.migu.mediadata.pojo.MediaActivity;
import com.ebupt.migu.music.bannertwo.entity.BannerTwoCheck;
import com.ebupt.migu.music.bannertwo.entity.BannerTwoInfo;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoCheckService;
import com.ebupt.migu.music.bannertwo.service.IBannerTwoInfoService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.EmptyUtils;
import com.ebupt.migu.music.media.entity.MediaCheck;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;



@RestController
@RequestMapping("/bannerTwoCheck")
@Slf4j
public class BannerTwoCheckController{
	@Autowired
	private IBannerTwoCheckService bannerTwoCheckService;
	@Autowired
	private IBannerTwoInfoService bannerTwoInfoService;
	/**
	 * 分页列表查询
	 *
	 * @param bannerTwoCheck
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public ResultObject<IPage<BannerTwoCheck>> queryPageList(BannerTwoCheck bannerTwoCheck,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BannerTwoCheck> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.eq(BannerTwoCheck::getDeleteType,0)
				.eq(!EmptyUtils.isEmpty(bannerTwoCheck.getBannerTwoId()), BannerTwoCheck::getBannerTwoId, bannerTwoCheck.getBannerTwoId())
				.like(!EmptyUtils.isEmpty(bannerTwoCheck.getBannerTwoName()), BannerTwoCheck::getBannerTwoName, bannerTwoCheck.getBannerTwoName())
				.eq(!EmptyUtils.isEmpty(bannerTwoCheck.getState()), BannerTwoCheck::getState, bannerTwoCheck.getState())
				.eq(!EmptyUtils.isEmpty(bannerTwoCheck.getCreateBy()), BannerTwoCheck::getProposer, bannerTwoCheck.getCreateBy())
				.eq(!EmptyUtils.isEmpty(bannerTwoCheck.getDepartment()), BannerTwoCheck::getDepartment, bannerTwoCheck.getDepartment())
			    .between(!EmptyUtils.isEmpty(bannerTwoCheck.getSTime()) && !EmptyUtils.isEmpty(bannerTwoCheck.getETime()), BannerTwoCheck::getCreateTime, bannerTwoCheck.getSTime(), bannerTwoCheck.getETime())
				.orderByDesc(BannerTwoCheck::getCreateTime);
		Page<BannerTwoCheck> page = new Page<BannerTwoCheck>(pageNo, pageSize);
		IPage<BannerTwoCheck> pageList = bannerTwoCheckService.page(page, queryWrapper);
		return ResultObject.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param bannerTwoCheck
	 * @return
	 */
	@PostMapping(value = "/add")
	public ResultObject<String> add(@RequestBody BannerTwoCheck bannerTwoCheck) {
		bannerTwoCheckService.save(bannerTwoCheck);
		return ResultObject.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param bannerTwoCheck
	 * @return
	 */
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public ResultObject<String> edit(@RequestBody BannerTwoCheck bannerTwoCheck) {
		bannerTwoCheckService.updateById(bannerTwoCheck);
		BannerTwoInfo bannerTwoInfo = bannerTwoInfoService.getById(bannerTwoCheck.getBannerTwoId());
		UpdateWrapper<BannerTwoInfo> bannerTwoInfoUpdateWrapper = new UpdateWrapper<>();
		bannerTwoInfoUpdateWrapper.lambda().eq(true, BannerTwoInfo::getId, bannerTwoCheck.getBannerTwoId());
		if (1==bannerTwoCheck.getState()) {
			if(DateUtil.date().isBefore(DateUtil.date(bannerTwoInfo.getStartTime()))){
				bannerTwoInfoUpdateWrapper.lambda().set(true, BannerTwoInfo::getExecutionStatus, 4);
			}else{
				bannerTwoInfoUpdateWrapper.lambda().set(true, BannerTwoInfo::getExecutionStatus, 3);
			}
		}

		if (2==bannerTwoCheck.getState()) {
			bannerTwoInfoUpdateWrapper.lambda().set(true, BannerTwoInfo::getExecutionStatus, 2);
		}
		bannerTwoInfoService.update(bannerTwoInfoUpdateWrapper);
		return ResultObject.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public ResultObject<String> delete(@RequestParam(name="id",required=true) String id) {
		bannerTwoCheckService.removeById(id);
		return ResultObject.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public ResultObject<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bannerTwoCheckService.removeByIds(Arrays.asList(ids.split(",")));
		return ResultObject.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "banner2审核-通过id查询")
	public ResultObject<?> queryById(@RequestParam(name="id",required=true) String id) {
		BannerTwoCheck bannerTwoCheck = bannerTwoCheckService.getById(id);
		if(bannerTwoCheck==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(bannerTwoCheck);
	}

	@GetMapping(value = "/queryByBId")
	public ResultObject<?> queryByBId(@RequestParam(name="BannerId",required=true) String id) {
		new QueryWrapper<BannerTwoCheck>().lambda().eq(BannerTwoCheck::getBannerTwoId,id);
		BannerTwoCheck bannerTwoCheck = bannerTwoCheckService.getOne(new QueryWrapper<BannerTwoCheck>().lambda().eq(BannerTwoCheck::getBannerTwoId,id));
		if(bannerTwoCheck==null) {
			return ResultObject.error("未找到对应数据");
		}
		return ResultObject.ok(bannerTwoCheck);
	}


}
